/*
 * @Descripttion:
 * @version:
 * @Author: wujq
 * @Date: 2023-08-02 17:12:16
 * @LastEditors: Andy
 * @LastEditTime: 2023-08-11 15:11:01
 */
import { createActions, handleActions } from 'redux-actions';
import { statusNetwork, testNetwork } from '@api/networkConfig';

import _ from 'lodash';

const initState = {
  messageList: [],
  netWorkLoding: false
};

export const actions = createActions({
  // 网络配置
  GET_STATUS_NETWORK: params => {
    return statusNetwork(params).then(data => {
      const messageList = _.get(data, 'data.data.networkStatusList') || [];
      if (messageList) {
        return Promise.resolve(data || []);
      }
    });
  },
  GET_STATUS_NETWORK_TEST: params => {
    return dispatch => {
      dispatch({ type: 'SET_TEST_NETWORK_LOADING', payload: true }); // 设置 testNetworkLoading 为 true
      return testNetwork(params)
        .then(data => {
          const messageList = _.get(data, 'data.data.networkStatusList') || [];
          if (messageList) {
            return Promise.resolve(messageList || {});
          }
        })
        .finally(() => {
          dispatch({ type: 'SET_TEST_NETWORK_LOADING', payload: false });
        });
    };
  }
});

const reducer = handleActions(
  {
    // 网络配置
    GET_STATUS_NETWORK: (state, actions) => {
      const networkStatusList = _.get(actions.payload, 'data.data.networkStatusList', []);
      return {
        ...state,
        messageList: networkStatusList
      };
    },
    // eslint-disable-next-line no-dupe-keys
    SET_TEST_NETWORK_LOADING: (state, actions) => {
      const { payload } = actions;
      return {
        ...state,
        netWorkLoding: payload
      };
    }
  },
  initState
);

export default reducer;
